package utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import managers.DBManager;

public class LoginRetrievingMethods {

	
	private LoginRetrievingMethods () {
	}
	
	
	public static Object[] getUsers (Connection conn, Boolean registered) throws SQLException {
		
		ArrayList<Object> a = new ArrayList<Object>();
		Statement stmt = conn.createStatement();
		String query = null;
		if (registered == null)
			query = "select id, firstname, surname from spotmessenger.USERS order by id";
		else if (registered == false)
			query = "select id, firstname, surname from spotmessenger.USERS where registered = 0 order by id";
		else
			query = "select id, firstname, surname from spotmessenger.USERS where registered = 1 order by id";
		
		ResultSet rs = stmt.executeQuery(query);
		while (rs.next()) {
			Object[] user = {rs.getInt(1), rs.getString(2)+" "+rs.getString(3)};
			a.add(user);
		}
		Object[] users = new Object[a.size()];
		a.toArray(users);
		DBManager.closeResources(stmt, rs);
		return users;
		
	}
	
	public static int NICKNAME = 0;
	public static int ID = 1;
	
	public static Object[] getPassword (Connection conn, String key, int from) throws SQLException{
		Statement stmt = conn.createStatement();
		String query = null;
		if (from == NICKNAME) {
			query = "select password, registered, id from spotmessenger.USERS where nickname = '"+key+"'";
		} else if (from == ID) {
			query = "select password, registered, id from spotmessenger.USERS where id = "+key;
		}
		ResultSet rs = stmt.executeQuery(query);
		rs.next();
		Object[] pswd = {rs.getString(1), rs.getBoolean(2), rs.getInt(3)};
		DBManager.closeResources(stmt, rs);
		return pswd;
	}
	
}
